import 'package:dio/dio.dart';
import 'package:palmf_hid/common/utils/net/http_request.dart';
import 'package:palmf_hid/common/utils/net/interceptor/cache.dart';
import 'package:palmf_hid/common/values/api_constants.dart';

import 'interceptor/response.dart';

class HttpUtils {
  static void init({
    required String baseUrl,
    int connectTimeout = 1000 * 10,
    int receiveTimeout = 1000 * 10,
    List<Interceptor>? interceptors,
  }) {
    Http().init(
      baseUrl: baseUrl,
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      interceptors: interceptors,
    );
  }

  static void cancelRequests({required CancelToken token}) {
    Http().cancelRequests(token: token);
  }

  static Future get(String path,
      {Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      bool refresh = false,
      bool noCache = !CACHE_ENABLE,
      String? cacheKey,
      bool cacheDisk = false,
      String? urlAddress = ApiConstants.hiproUrl}) async {
    Http().changeBaseUrl(urlAddress ?? ApiConstants.hiproUrl);
    return await Http().get(
      path,
      params: queryParameters,
      options: options,
      cancelToken: cancelToken,
      refresh: refresh,
      noCache: noCache,
      cacheKey: cacheKey,
    );
  }

  static Future post(String path,
      {data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      String? urlAddress = ApiConstants.hiproUrl}) async {
    Http().changeBaseUrl(urlAddress ?? ApiConstants.hiproUrl);
    return await Http().post(
      path,
      data: data,
      params: queryParameters,
      options: options,
      cancelToken: cancelToken,
    );
  }

  static Future put(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return await Http().put(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
    );
  }

  static Future patch(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return await Http().patch(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
    );
  }

  static Future delete(
    String path, {
    data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return await Http().delete(
      path,
      data: data,
      params: params,
      options: options,
      cancelToken: cancelToken,
    );
  }

  static Future<Response> download(String urlPath, savePath,
      {ProgressCallback? onReceiveProgress,
      Map<String, dynamic>? queryParameters,
      CancelToken? cancelToken,
      bool deleteOnError = true,
      String lengthHeader = Headers.contentLengthHeader,
      data,
      Options? options}) async {
    try {
      var response = await Http().download(
        urlPath,
        savePath,
        onReceiveProgress: onReceiveProgress,
        queryParameters: queryParameters,
        cancelToken: cancelToken,
        deleteOnError: deleteOnError,
        lengthHeader: lengthHeader,
        data: data,
        options: data,
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }
}

class ResponseData extends BaseResponseData {
  bool get success => (code == 0 || code == 200);

  ResponseData.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    msg = json['msg'];
    data = json['data'];
  }

  ResponseData.fromResponse(Response response) {
    code = response.data['code'];
    msg = response.data['msg'];
    data = response.data;
  }
}
